﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using QXJ.DBUtility;
using System.Data;
using System.Diagnostics;

namespace QXJ
{
    enum DataField
    {
        quzhanhao =0,
        weidu = 1,
        jingdu = 2,
        guanchehaibagaoya  = 3,
        qiyachuanganqihaibagaodu = 4,
        guanchefangshi = 5,
        guancheshijian = 6,
        liangfenzhongfengfengxiang= 7,
        lianfenzhongfengxiang = 8,

    };
    class Program
    {

        static void OutDataTable(DataTable dt)
        {
            for (var i = 0; i < dt.Rows.Count; i++)
            {
                for (var j = 0; j < dt.Columns.Count; j++)
                {
                    Console.Write(dt.Rows[i][j]);
                    Console.Write("   |   ");
                }
                Console.WriteLine();
            }
        }


        static void InsertOneLine(string s1 ,string s2,string s3)
        {
            string[] line1 = s1.Split(' ');
            string[] line2 = s2.Split(' ');
            string[] line3 = s3.Split(' ');
            if (line1.Length!=6 || line2.Length != 52 || line3.Length!=1)
            {
                return;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("insert into qxj(`quzhanhao`,`weidu`,`jingdu`,`HightOfGuanCheChang`,`HightOfQiYaChuanGanQi`,`TypeOfGuanChe`,`Time`,`TwoFZFFX`,`TwoFXxiang`,`TenFXiang`,`TenFSpeed`,`MaxFXiang`,`MaxFSpeed`,`TimeOfMaxF`,`SunShiFXiang`,`SunShiFSpeed`,`JiDaFXiang`,`JiDaFSpeed`,`TimeOfJiDaF`,`RainVolPerHour`,`TempOfGanQiu`,`MaxWenDu`,`TimeOfMaxCuXian`,`MinWenDu`,`TimeOfMinWenDuCuXian`,`XiangDuiShiDu`,`MinXiangDuiShiDu`,`TimeOfMinShiDuCuXian`,`BenZhanQiYa`,`MaxOfBenZhan`,`TimeOfMaxOfBenZhan`,`LowestOfOur`,`TimeOfLowest`,`VolOfRainOneHour`) values(");
            for (int i = 0; i < line1.Length; i++)
            {
                
                sb.Append('\'');
                sb.Append(line1[i].Trim());
                sb.Append('\'');
                sb.Append(" , ");
            }
            for (int i = 0; i < 29; i++)
            {
                if (i == 22 || i == 23 )//空值 
                {
                    continue;
                }
                if (i==13)//雨量，计算出来的
                {
                    int vol = 0;
                    int tmpvol = 0;
                    StringBuilder tmpsb = new StringBuilder();
                    if (line3[0].Replace("=", "").Trim().Length == 120)
                    {
                        for (int lei = 0; lei < 119; lei+=2)
                        {
                            tmpsb.Append(line3[0].ElementAt(lei));
                            tmpsb.Append(line3[0].ElementAt(lei + 1));
                            int.TryParse(tmpsb.ToString(),out  tmpvol);
                            vol += tmpvol;
                            tmpvol = 0;
                            tmpsb.Clear();
                        }
                       
                    }
                    sb.Append('\'');
                    sb.Append(vol.ToString());
                    sb.Append('\'');
                    sb.Append(" , ");
                    continue;
                }
                sb.Append('\'');
                sb.Append(line2[i].Trim());
                sb.Append('\'');
                sb.Append(" , ");
            }

            //就一行
            sb.Append('\'');
            sb.Append(line3[0].Replace("=","").Trim());
            sb.Append('\'');
            sb.Append(")");
            Console.WriteLine(sb.ToString());
            DataTable dt = DataAccess.GetDataTable("select * from qxj where `quzhanhao`= 'I1353'");
            OutDataTable(dt);
            //DataAccess.ExecuteCmd(sb.ToString());
            sb.Clear();
            return;
        }
        static void Main(string[] args)
        {
            Stopwatch sw = Stopwatch.StartNew();
            Console.WriteLine(Environment.CurrentDirectory);
            string path = Path.Combine(Environment.CurrentDirectory, @"D:\my-gw\qxj\file\z\Z_SURF_C_BEHF-REG_20130412220051_O_AWS_FTM.TXT");
            StringBuilder sb = new StringBuilder(File.ReadAllText(path, Encoding.Default));

            String[] spsb = sb.ToString().Split('\r');

            for (int i = 0; i < spsb.Length-2; i++)
            {
                InsertOneLine(spsb[i], spsb[i + 1], spsb[i + 2]);
                break;
            }
            //int i = 3;
            //int current =0;
            //foreach (var item in spsb)
            //{
            //    current++;
            //    Console.WriteLine("\nOne:");
            //    Console.WriteLine(item);
            //    Console.WriteLine("分开：");
            //    string[] s = item.Split(' ');
            //    Console.WriteLine("Len:{0}",s.Length);
            //    foreach (var isa in s)
            //    {
            //        Console.Write("[{0}]",isa.Trim().Replace("=","").Trim());
            //    }
            //    if (i == current )
            //    {
            //       // Console.ReadKey();
            //        current = i;
            //        i += 3;
            //    }
  
            //}
            //string path = Path.Combine(Environment.CurrentDirectory, "a.html");
            //StringBuilder htmlcontext = new StringBuilder(
            //    File.ReadAllText(path, Encoding.UTF8)
            //);

            //Console.WriteLine(htmlcontext.ToString());
            //if (!File.Exists("b.html")) //不用判断
            //{
            //    File.CreateText("b.html");
            //}

            //string head = "{%Rain";
            //string end = "%}";
            ////string val = string.Empty;
            //StringBuilder sb = new StringBuilder();
            //for (int i = 0; i <= 60; i++)
            //{
            //    sb.Append(head);
            //    sb.Append(i.ToString());
            //    sb.Append(end);//= head + i.ToString() + end;
            //    htmlcontext.Replace(sb.ToString(), "00");
            //    sb.Clear();
            //}
            //File.WriteAllText("b.html",htmlcontext.ToString());
            //DataAccess.Open();
            //DataTable dt = DataAccess.GetDataTable("select * from [User]");
            //OutDataTable(dt);
            //DataAccess.ExecuteCmd("insert into [user](`id`,`user`,`pwd`,`backup`) values(103,'zsb','zsb','nooo')");
            //dt.Clear();
            //dt = DataAccess.GetDataTable("select * from [user]");
            //OutDataTable(dt);
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
            Console.WriteLine("Finished");
            Console.ReadKey();
            
        }
    }
}
